package com.amumu.netctoss.dao.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.springframework.context.annotation.Scope;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.amumu.netctoss.dao.ServiceDao;
import com.amumu.netctoss.entity.Account;
import com.amumu.netctoss.entity.Service;
@Repository
@Scope("prototype")
public class HibernateServiceDao extends HibernateBaseDao implements ServiceDao {

	public void delete(int id) throws Exception {
		Service s = findById(id);
		s.setStatus("2");
		s.setCloseDate(new Date(System.currentTimeMillis()));
		getHibernateTemplate().update(s);
	}

	public Service findByHostAndUsername(String unixHost, String username)
			throws Exception {
		return (Service) uniqueResult(
				"from Service where unixHost=? and osUsername=?", 
				unixHost,username);
	}

	public Service findById(int id) throws Exception {
		return (Service) getHibernateTemplate().load(Service.class, id);
	}

	public int getPageCount(int size, String username, String unixHost,
			String idcardNo, String status) throws Exception {
		List params = new ArrayList();
		StringBuilder sql = new StringBuilder("select count(*) from Service s join s.account a");
	
		if(!"".equals(username)&&username!=null){
			sql.append(" and s.osUsername=? ");
			params.add(username);
		}
		if(!"".equals(unixHost)&&unixHost!=null){
			sql.append(" and s.unixHost=? ");
			params.add(unixHost);
		}
		if(!"".equals(idcardNo)&&idcardNo!=null){
			sql.append(" and a.idcardNo=? ");
			params.add(idcardNo);
		}
		if(!"".equals(status)&&status!=null){
			sql.append(" and s.status=? ");
			params.add(status);
		}
		return getTotalPage(size, sql.toString(), params.toArray());
	}

	public Map<String, Object> getStatusOptions() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("0", "开通");
		map.put("1", "暂停");
		map.put("2", "删除");
		return map;
	}

	public boolean hasHost(final String host) throws Exception {
		Object obj = getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException{
				Query query = session.createQuery("select id from host where id=?");
				query.setParameter(0,host.trim());
				return query.uniqueResult();
			}
		});
		return obj!=null;
	}

	public void save(Service service) throws Exception {
		service.setCreateDate(new Date(System.currentTimeMillis()));
		service.setStatus("0");
		getHibernateTemplate().save(service);
		
	}

	public List<Service> search(int page, int size, String username,
			String unixHost, String idcardNo, String status) throws Exception {
		
		List params = new ArrayList();
		StringBuilder sql = new StringBuilder("from Service s join s.account a");
	
		if(!"".equals(username)&&username!=null){
			sql.append(" and s.osUsername=? ");
			params.add(username);
		}
		if(!"".equals(unixHost)&&unixHost!=null){
			sql.append(" and s.unixHost=? ");
			params.add(unixHost);
		}
		if(!"".equals(idcardNo)&&idcardNo!=null){
			sql.append(" and a.idcardNo=? ");
			params.add(idcardNo);
		}
		if(!"".equals(status)&&status!=null){
			sql.append(" and s.status=? ");
			params.add(status);
		}
		
		List list = findByPage(page, size, sql.toString(), params.toArray());
		List<Service> services = new ArrayList<Service>();
		Service service;Account account;
		for(int i = 0; i < list.size();i++){
			Object[] object = (Object[])list.get(i);
			service =  (Service)object[0];
			service.setAccount((Account)object[1]);
			services.add(service);
		}
		return services;
	}

	@Transactional
	public void setStatus(int id, String status) throws Exception {
		Service s = findById(id);
		s.setStatus(status);
		if(status.equals("0")){
			s.setPauseDate(null);
		}else{
			s.setPauseDate(new Date(System.currentTimeMillis()));
		}
		getHibernateTemplate().update(s);
		
	}

	public void update(final Service service) throws Exception {
		getHibernateTemplate().execute(new HibernateCallback() {
			
			public Object doInHibernate(Session session) throws HibernateException{
				NativeQuery query = session.createNativeQuery(
						"INSERT INTO SERVICE_UPDATE_BAK (SERVICE_ID,COST_ID)values(?,?)");
				query.setParameter(1, service.getId());
				query.setParameter(2, service.getCost().getId());
				return query.executeUpdate();
			}
		});
		
	}

}
